package example.dao;


import example.entity.Fine;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 张心怡
 * @date 2024/12/16
 */
public class FineDao extends BaseDAO {
    public static void delFine(int fineId) {
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement("delete FROM fine WHERE fine_id =?")) {
            statement.setInt(1, fineId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void addFine(int userId, int bookId, double amount, int borrowId) {
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement("INSERT INTO fine (user_id,book_id, amount, due_date, paid,borrow_id) VALUES (?,?,?,?,?,?)")) {
            statement.setInt(1, userId);
            statement.setInt(2, bookId);
            statement.setDouble(3, amount);
            // 假设罚款期限为7天
            statement.setDate(4, new Date(System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000));
            statement.setBoolean(5, false);
            statement.setInt(6, borrowId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Fine getFinesByBorrowId(int borrowId) {
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM fine WHERE borrow_id =?")) {
            preparedStatement.setInt(1, borrowId);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    return new Fine(
                            resultSet.getInt("fine_id"),
                            resultSet.getInt("user_id"),
                            resultSet.getInt("book_id"),
                            resultSet.getDouble("amount"),
                            resultSet.getDate("due_date"),
                            resultSet.getBoolean("paid"),
                            resultSet.getInt("borrow_id")
                    );
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void payAmount(double amount, int fineId) {
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement("UPDATE fine SET amount = ? WHERE fine_id =?")) {
            statement.setDouble(1, amount);
            statement.setInt(2, fineId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static List<Fine> getFinesByUserId(int userId) {
        List<Fine> fines = new ArrayList<>();
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement("SELECT * FROM fine WHERE user_id =?")) {
            statement.setInt(1, userId);
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    Fine fine = new Fine(
                            resultSet.getInt("fine_id"),
                            resultSet.getInt("user_id"),
                            resultSet.getInt("book_id"),
                            resultSet.getDouble("amount"),
                            resultSet.getDate("due_date"),
                            resultSet.getBoolean("paid"),
                            resultSet.getInt("borrow_id")
                    );
                    fines.add(fine);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fines;
    }

    public static List<Fine> getFinesAll() {
        List<Fine> fines = new ArrayList<>();
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement("SELECT * FROM fine")) {
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    Fine fine = new Fine(
                            resultSet.getInt("fine_id"),
                            resultSet.getInt("user_id"),
                            resultSet.getInt("book_id"),
                            resultSet.getDouble("amount"),
                            resultSet.getDate("due_date"),
                            resultSet.getBoolean("paid"),
                            resultSet.getInt("borrow_id")
                    );
                    fines.add(fine);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fines;
    }

    public static void payFine(int fineId) {
        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement("UPDATE fine SET paid = true WHERE fine_id =?")) {
            statement.setInt(1, fineId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
